package com.example.xunai.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

@Configuration
public class JacksonConfig {

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        ObjectMapper objectMapper = new ObjectMapper();

        // 1. 支持Java 8日期时间类型
        objectMapper.registerModule(new JavaTimeModule());

        // 2. 支持Hibernate代理对象序列化
        objectMapper.registerModule(new Hibernate5JakartaModule());

        // 3. 禁用空Bean序列化失败的特性
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        // 4. 可选：禁用日期时间作为时间戳序列化
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        return new MappingJackson2HttpMessageConverter(objectMapper);
    }
}
